#include "stdafx.h"
#include "NWString.h"
#include <winnt.h>
#include <winnls.h>

NWString::NWString()
{
	//Length = 0;
	Initialize();
}

NWString::NWString(LPTSTR lpBuffer)
{
	if(lpBuffer)
	{
		int Length = wcslen(lpBuffer);
		String = new WCHAR[Length + 1];
		wcscpy(String,lpBuffer);
		String[Length] = 0;
	}
	else Initialize();
}

NWString::NWString(WCHAR Char)
{
	//Length = 1;
	String = new WCHAR[2];
	String[0] = Char;
	String[1] = 0;
}

NWString::NWString(NWString& Buffer)
{
	if(Buffer.String)
	{
		int Length = wcslen(Buffer.String);
		String = new WCHAR[Length + 1];
		wcscpy(String,Buffer.String);
		String[Length] = 0;
	}
	else Initialize();
}

NWString::~NWString()
{
	Clear();
}

NWString NWString::operator=(LPTSTR lpBuffer)
{
	Clear();
	int Length = wcslen(lpBuffer);
	String = new WCHAR[Length + 1];
	wcscpy(String,lpBuffer);
	String[Length] = 0;
	return *this;
}

NWString NWString::operator=(NWString& Buffer)
{
	Clear();
	int Length = wcslen(Buffer.String);
	String = new WCHAR[Length + 1];
	wcscpy(String,Buffer.String);
	String[Length] = 0;
	return *this;
}

NWString NWString::operator=(WCHAR Char)
{
	Clear();
	String = new WCHAR[2];
	String[0] = Char;
	String[1] = 0;
	return *this;
}

void NWString::operator++(int)
{
	int Length = wcslen(String);
	LPTSTR Result = new WCHAR[Length];
	wcscpy(Result,&String[1]);
	Result[Length - 1] = 0;
	*this = Result;
	delete[] Result;
}

NWString NWString::operator+=(int Count)
{
	int Length = wcslen(String);
	LPTSTR Result = new WCHAR[Length - Count + 1];
	wcscpy(Result,&String[Count]);
	Result[Length - Count] = 0;
	NWString Temp = Result;
	delete[] Result;
	return Temp;
}

NWString NWString::operator+=(LPTSTR lpBuffer)
{
	NWString Temp1 = String;
	NWString Temp2 = lpBuffer;
	NWString Temp = Temp1 + Temp2;
	Clear();
	int Length = wcslen(Temp.String);
	String = new WCHAR[Length + 1];
	wcscpy(String,Temp.String);
	return *this;
}

NWString NWString::operator+=(NWString& Buffer)
{
	NWString Temp1 = String;
	NWString Temp = Temp1 + Buffer;
	Clear();
	int Length = wcslen(Temp.String);
	String = new WCHAR[Length + 1];
	wcscpy(String,Temp.String);
	return *this;
}

/*
NWString NWString::operator+=(WCHAR Char)
{
	NWString Temp1 = String;
	NWString Temp2 = Char;
	NWString Temp = Temp1 + Temp2;
	Clear();
	int Length = wcslen(Temp.String);
	String = new WCHAR[Length + 1];
	wcscpy(String,Temp.String);
	return *this;
}
*/

NWString NWString::operator+(LPTSTR lpBuffer)
{
	NWString Temp1 = String;
	NWString Temp2 = lpBuffer;
	return Temp1 + Temp2;
}

NWString NWString::operator+(NWString& Buffer)
{
	int Length = wcslen(String);
	LPTSTR Result = new WCHAR[Length + wcslen(Buffer.String) + 1];
	wcscpy(Result,String);
	wcscat(Result,Buffer.String);
	Result[Length + wcslen(Buffer.String)] = 0;
	NWString Temp = Result;
	delete[] Result;
	return Temp;
}

NWString NWString::operator+(WCHAR Char)
{
	NWString Temp1 = String;
	NWString Temp2 = Char;
	return Temp1 + Temp2;
}

BOOL NWString::operator==(LPTSTR lpBuffer)
{
	int Length = wcslen(String);
	if(Length != wcslen(lpBuffer)) return FALSE;
	return wcscmp(String,lpBuffer) == 0;
}

BOOL NWString::operator==(NWString& Buffer)
{
	return *this == Buffer.String;
}

BOOL NWString::operator==(WCHAR Char)
{
	return *this == &Char;
}

BOOL NWString::operator!=(LPTSTR lpBuffer)
{
	return (*this == lpBuffer) ^ 1;
}

BOOL NWString::operator!=(NWString& Buffer)
{
	return (*this == Buffer.String) ^ 1;
}

BOOL NWString::operator!=(WCHAR Char)
{
	return (*this == &Char) ^ 1;
}

WCHAR NWString::operator[](int Index)
{
	return String[Index];
}

void NWString::Insert(int Start,LPTSTR lpBuffer)
{
	NWString Temp = lpBuffer;
	Insert(Start,Temp);
}

void NWString::Insert(int Start,NWString& Buffer)
{
	int Length = wcslen(String);
	if(Start >= 0 && Start <= Length - 1)
	{
		NWString Temp1 = Left(Start);
		NWString Temp2 = Right(Length - Start);
		NWString Temp = Temp1 + Buffer + Temp2;
		Clear();
		Length = wcslen(Temp.String);
		String = new WCHAR[Length + 1];
		wcscpy(String,Temp.String);
		String[Length] = 0;
	}
}

void NWString::Insert(int Start,WCHAR Char)
{
	NWString Temp = Char;
	Insert(Start,Temp);
}

void NWString::Delete(int Start,int Count /* = -1 */)
{
	int Length = wcslen(String);
	if(Count == -1)
	{
		if(Start < 0 || Start > Length - 1)
			return;
		NWString Temp = Left(Start);
		Clear();
		Length = wcslen(Temp.String);
		String = new WCHAR[Length + 1];
		wcscpy(String,Temp.String);
		String[Length] = 0;
	}
	else
	{
		if(Start < 0 || Start > Length - 1 || Count <= 0 || Count > Length - Start)
			return;
		NWString Temp1 = Left(Start);
		NWString Temp2 = Right(Length - Start - Count);
		NWString Temp = Temp1 + Temp2;
		Clear();
		Length = wcslen(Temp.String);
		String = new WCHAR[Length + 1];
		wcscpy(String,Temp.String);
		String[Length] = 0;
	}
}

void NWString::Format(LPTSTR lpBuffer,...)
{
	va_list list;
	va_start(list,lpBuffer);
	FormatV(lpBuffer,list);
	va_end(list);
}

void NWString::FormatV(LPTSTR lpBuffer,va_list list)
{
	Clear();
	int Length = _vscwprintf(lpBuffer,list);
	String = new WCHAR[Length + 1];
	vswprintf_s(String,Length + 1,lpBuffer,list);
}

void NWString::Clear()
{
	if(String)
	{
		delete[] String;
		String = 0;
	}
}

NWString NWString::Sub(int Start,int Count /* = -1 */)
{
	int Length = wcslen(String);
	if(Count == -1)
	{
		if(Start < 0 || Start > Length - 1)
		{
			NWString Temp = L"";
			return Temp;
		}
		LPTSTR Result = new WCHAR[Length - Start + 1];
		wcscpy(Result,&String[Start]);
		Result[Length - Start] = 0;
		NWString Temp = Result;
		delete[] Result;
		return Temp;
	}
	else
	{
		if(Start < 0 || Start > Length - 1 || Count <= 0 || Count > Length - Start)
		{
			NWString Temp = L"";
			return Temp;
		}
		LPTSTR Result = new WCHAR[Count + 1];
		wcsncpy(Result,&String[Start],Count);
		Result[Count] = 0;
		NWString Temp = Result;
		delete[] Result;
		return Temp;
	}
}

NWString NWString::Left(int Count)
{
	return Sub(0,Count);
}

NWString NWString::Right(int Count)
{
	int Length = wcslen(String);
	return Sub(Length - Count);
}

NWString NWString::ToUpper()
{
	String = wcsupr(String);
	return *this;
}

NWString NWString::ToLower()
{
	String = wcslwr(String);
	return *this;
}

NWString NWString::TrimLeft()
{
	int Length = wcslen(String);
	int Count = 0;
	LPTSTR lpBuffer = String;
	while(*lpBuffer && *lpBuffer == ' ')
	{
		lpBuffer++;
		Count++;
	}
	NWString Temp = Right(Length - Count);
	return Temp;
}

NWString NWString::TrimRight()
{
	int Length = wcslen(String);
	int Count = 0;
	LPTSTR lpBuffer = String;
	while(*lpBuffer)
		lpBuffer++;
	while(lpBuffer != String && *lpBuffer == ' ')
	{
		lpBuffer--;
		Count++;
	}
	NWString Temp = Left(Length - Count);
	return Temp;
}

NWString NWString::Trim()
{
	return TrimLeft().TrimRight();
}

LPTSTR NWString::GetBuffer()
{
	return String;
}

int NWString::GetLength()
{
	return wcslen(String);
}

int NWString::Find(WCHAR Char)
{
	int Length = wcslen(String);
	for(int i=0;i<Length;i++)
		if(String[i] == Char)
			return i;
	return -1;
}

int NWString::FindLast(WCHAR Char)
{
	int Length = wcslen(String);
	for(int i=Length-1;i>=0;i--)
		if(String[i] == Char)
			return i;
	return -1;
}

LPTSTR NWString::ToANSI()
{
	int ansiLen = WideCharToMultiByte(CP_ACP, NULL, String, wcslen(String), NULL, 0, NULL, NULL);
	LPSTR Result = new CHAR[ansiLen + 1];
	WideCharToMultiByte(CP_ACP, NULL, String, wcslen(String), Result, ansiLen, NULL, NULL);
	Result[ansiLen] = 0;
	Clear();
	String = (LPTSTR)Result;
	return String;
}

void NWString::Initialize()
{
	String = new WCHAR[1];
	wcscpy(String,L"");
}